
package com.kdgc.power.common.util;


import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
    public static String toUnderline(String str) {
        String regex = "([A-Z])";
        String target;
        for (Matcher matcher = Pattern.compile(regex).matcher(str); matcher.find(); str = str.replaceAll(target, "_" + target.toLowerCase())) {
            target = matcher.group();
        }

        return str.startsWith("_") ? str.substring(1) : str;
    }

    public static String toBigHump(String str) {
        String regex = "_(.)";
        String target;
        str = str.toLowerCase();
        for (Matcher matcher = Pattern.compile(regex).matcher(str); matcher.find();
             str = str.replaceAll("_" + target, target.toUpperCase())) {
            target = matcher.group(1);
        }
        return String.valueOf(str.charAt(0)).toUpperCase() + str.substring(1);
    }

    public static String toSmallHump(String str) {
        String regex = "_(.)";
        String target;
        str = str.toLowerCase();
        for (Matcher matcher = Pattern.compile(regex).matcher(str); matcher.find();
             str = str.replaceAll("_" + target, target.toUpperCase())) {
            target = matcher.group(1);
        }
        return str;
    }

    /**
     * 拼音 -->  pīn yīn
     *
     * @param str 带转换为拼音的汉字字符串
     */
    public static void toPinyin(String str) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        try {
            str = PinyinHelper.toHanYuPinyinString(str, format, " ", true);
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            throw new RuntimeException(e);
        }
        System.out.println(str);
    }

    public static String toString(Object obj) {
        return isEmpty(obj) ? "" : obj.toString();
    }

    public static int toInt(Object obj) {
        try {
            return Integer.parseInt(toString(obj));
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    public static long toLong(Object obj) {
        try {
            return Long.parseLong(toString(obj));
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    public static String nvl(Object obj, String replace) {
        return isEmpty(obj) ? replace : obj.toString();
    }

    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    public static boolean isEmpty(Object obj) {
        return obj == null || "null".equalsIgnoreCase(obj.toString().trim()) || obj.toString().trim().isEmpty();
    }


    public static String md5(String pwd) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");// 生成一个MD5加密计算摘要
            md.update(pwd.getBytes());// 计算md5函数
            return new BigInteger(1, md.digest()).toString(16);// 16是表示转换为16进制数
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String md5(String pwd, int count) {
        String result = "";
        for (int i = 0; i < count; i++) {
            assert result != null;
            result = md5(result);
        }
        return result;
    }

    public static void print(Object... obj) {
        if (StringUtil.isNotEmpty(obj) && obj.length > 0) {
            Arrays.stream(obj).forEach(o -> System.out.print(o + " "));
        }
        System.out.println();
    }

    public static void print(Map<?, ?> map) {
        map.forEach((key, value) -> System.out.println(key + "  ====>>>    " + value));
    }
    public static void print(Collection <?> collection) {
       collection.forEach(System.out::println);
    }
}
